#!/bin/bash
# Verify forward zone file as well as reverse zone
# Author: Albert Xu <yoxu@ebay.com> on Nov. 2011
# $Id: zone_fileaudit.sh,v 1.2 2014/03/28 05:53:23 Albert Exp $
###########################################################
# $Log: zone_fileaudit.sh,v $
# Revision 1.2  2014/03/28 05:53:23  Albert
# added vip checking.
# using new getent command
#
###########################################################

# TO="yoxu@ebay.com"
TO="itunix@ebay.com"

MAIL=/var/tmp/dns_verify.txt
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/bind/bin:/usr/local/sbin:/usr/local/bin:/usr/sfw/bin
cat /dev/null >$MAIL
ZONEDIR=/var/named/db/its
[ ! -d "$ZONEDIR" ] && echo "Error: $ZONEDIR does not exist!" && exit 1

# Define console address we do not care
CONSOLE_EXCLUDE="|cyc|pphdp|iport|hitcon|castiron|itlb|puma|itjs|ppntz|mbeta|marcie|woodstock|snoopy|esx|itpsc|phxit-fab|ams2|hus130|vsp|sec|hnas"

cd $ZONEDIR
GET="getent hosts "

# Console Check
# Excluded: Haddop, Cyclades, Iport, Hitcon, Cast
echo "Checking Console regiter status..."
MSG=""
MSG_HEAD="Pls check to register console address(con/ilo.its.ebay.com) for below hosts:"
for SITE in  den sjc phx dub oma bom sjn sha slc
do
  ZONEFILE=db.$SITE.its.ebay.com
  [ ! -f $ZONEFILE ] && continue
  for HOST in `grep -w "A"  $ZONEFILE|awk '{print $1}'|egrep -v $CONSOLE_EXCLUDE`
  do
	[ -z "`$GET $HOST.con.its.ebay.com`" ] && [ -z "`$GET $HOST.ilo.its.ebay.com`" ] && [ -z "`$GET $HOST.ilo.corp.ebay.com`" ] && MSG="\t$HOST.$SITE.its.ebay.com\n$MSG"
   done
done
[ "$MSG" !=  "" ] && echo -e "$MSG_HEAD \n$MSG\n" >>$MAIL

echo "Checking vip.its.ebay.com zone file..."
ZONEFILE=db.vip.its.ebay.com
MSG=""
MSG_HEAD="One or more vip.its.ebay.com CNAME point to below record(s) no longer exist!"
for R in `awk '/CNAME/ {print $NF}' $ZONEFILE`
do
	[ -z "`$GET $R`" ] && MSG="\t`grep -w $R $ZONEFILE`\n$MSG"
done
[ "$MSG" !=  "" ] && echo -e "$MSG_HEAD \n$MSG\n" >>$MAIL


echo "Checking Reverse zone file..."
REVERSE=named.rev-*
MSG=""
MSG_HEAD="Pls check to add foward for below records:"
for HOST in  `grep -w "PTR" $REVERSE 2>/dev/null|awk ' $2 == "PTR" {print "host", $3}; $3 == "PTR" {print "host",$4}'|sh|grep -w NXDOMAIN|awk '{print $2}'`
do
	MSG="\t`grep -w $HOST $REVERSE` \n$MSG"
done
[ "$MSG" !=  "" ] && echo -e "$MSG_HEAD \n$MSG\n" >>$MAIL

echo "Checking Forward zone file..."
MASTER_REVERSE=`ls named.rev-* 2>/dev/null|awk -F- '{print $2}'`
ZONEFILE=db.*.its.ebay.com
MSG=""
MSG_HEAD="Pls verify to add below reverse record:"
for IP in  `grep -w "A" $ZONEFILE 2>/dev/null|awk ' $3 == "A" {print "host", $4}; $4 == "A" {print "host",$5}'|sh|grep -w NXDOMAIN|awk -F. '{print $4 "." $3 "." $2 "." $1}'|sed "s/Host //g"|sort -n`
do
  NET=`echo $IP|awk -F. '{print $1 "." $2 "." $3}'`
  if echo "$MASTER_REVERSE"|grep -w "$NET" 1>/dev/null; then
	MSG="\t`grep -w $IP $ZONEFILE` \n$MSG"
  fi
done
[ "$MSG" !=  "" ] && echo -e "$MSG_HEAD \n$MSG\n" >>$MAIL

[ -s "$MAIL" ] && ls -al $MAIL && cat $MAIL|mailx -s "[unixmon_audit]DNS Zone File Audit" $TO && echo "Audit Result mailed to $TO" || echo "Very Nice! All Good!"
